from flask import *
import requests
import json
app = Flask(__name__)
@app.route('/')
def hello_world():
    return redirect('/index')

@app.route('/index')
def index():
    center_list = center()
    topdata=top()
    chian_data=china()
    a,b,c=history()
    return render_template('main.html',center_list=center_list,topdata=topdata,chian_data=chian_data,a=a,b=b,c=c)

if __name__ == '__main__':
    app.run()

def listData():     #获取到了全国的当天的疫情数据
    url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
    r = requests.get(url)
    data = json.loads(r.text)
    list_data = data['data']
    test_data = json.loads(list_data)
    return test_data

def center():   #中间
    testdata=listData()
    return testdata['chinaTotal']

def top():      #获取到了top10的数据
    test_data = listData()
    topdata={}
    for i in range(0, 10):
        topdata[test_data['areaTree'][0]['children'][i]['name']]=test_data['areaTree'][0]['children'][i]['total']['nowConfirm']

    return topdata
def china():      #china
    test_data = listData()
    china_data={}
    for i in range(0, 24):
        china_data[test_data['areaTree'][0]['children'][i]['name']]=test_data['areaTree'][0]['children'][i]['total']['nowConfirm']
        # print(test_data['areaTree'][0]['children'][i]['name'], test_data['areaTree'][0]['children'][i]['total']['nowConfirm'])
    return china_data

def history():
    url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_other"
    r = requests.get(url)
    data = json.loads(r.text)
    list_data = data['data']
    test_data = json.loads(list_data)
    lishi = test_data['chinaDayList']
    order_list = list(reversed(lishi))
    a=[]
    b=[]
    c=[]
    for r in range(7):
              a.append(order_list[r]['date'])       #时间
              b.append(order_list[r]['confirm'])    #累计确诊
              c.append( order_list[r]['heal'])      #累计治愈
    return a,b,c